package com.example.controller;


import com.example.Exception.*;
import com.example.model.Admin;
import com.example.model.Menu;
import com.example.model.Result;
import com.example.service.IAdminService;
import com.example.service.IMenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hello
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/admin")
@Api
public class AdminController {
    @Autowired
    IAdminService adminService;
    @Autowired
    IMenuService menuService;

    @PostMapping("/login")
    public Result<String> login(@RequestBody Admin admin) {
        try {
            Admin admin1 = adminService.login(admin);
        } catch (UnregisteredException e) {
            return Result.fail("account is unregistered!");
        } catch (ErrorPasswordOrAccountException e) {
            return Result.fail("password or account is wrong!");
        } catch (UserNotExistException e) {
            return Result.fail("user not exist!");
        }
        return Result.success("成功");
    }
    @PostMapping("/register")
    public Result<String> register(@RequestBody Admin admin) {
        try {
            adminService.Register(admin);
        } catch (RegisteredException e) {
            return Result.fail("account is registered!");
        } catch (FailToRegisterException e) {
            return Result.fail("fail to register!");
        }
        return Result.success("成功");
    }
    @PostMapping("/addDish")
    public Result<String> addDish(@RequestBody Menu menu){
        Boolean aBoolean = menuService.addDish(menu);
        return aBoolean ? Result.success("成功") : Result.fail("失败（店铺已有该菜品）");
    }
    @PostMapping("/deleteDish")
    public Result<String> deleteDish(@RequestBody Menu menu){
        Boolean aBoolean = menuService.deleteDish(menu);
        return aBoolean ? Result.success("成功") : Result.fail("失败");
    }
}
